package com.lengyu.vxminiprogram.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lengyu.vxminiprogram.common.AuthUtil;
import com.lengyu.vxminiprogram.common.BasePageParamDto;
import com.lengyu.vxminiprogram.common.HttpResultDto;
import com.lengyu.vxminiprogram.dto.SecretRealmDto;
import com.lengyu.vxminiprogram.entity.Car;
import com.lengyu.vxminiprogram.entity.News;
import com.lengyu.vxminiprogram.entity.SecretRealm;
import com.lengyu.vxminiprogram.entity.TakePeople;
import com.lengyu.vxminiprogram.security.rest.dto.JwtUser;
import com.lengyu.vxminiprogram.service.INewsService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Paths;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuyu
 * @since 2024-12-23
 */
@RestController
@RequestMapping("/news")
public class NewsController {
    private final static Logger logger = LoggerFactory.getLogger(SecretRealmController.class);
    @Autowired
    INewsService newsService;

    @PostMapping(value="/save")
    public HttpResultDto<String> save(@RequestBody News news){
        newsService.saveOrUpdate(news);
        return HttpResultDto.success(news.getId());
    }

    @PostMapping(value="/page")
    public HttpResultDto<Page<News>> page(@RequestBody BasePageParamDto pageParamDto){
        QueryWrapper queryWrapper = new QueryWrapper();
        if(pageParamDto.getParamMapObj() != null){
            for(String key:pageParamDto.getParamMapObj().keySet()){
                if(StringUtils.isNotBlank(String.valueOf(pageParamDto.getParamMapObj().get(key)))){
                    queryWrapper.like(key,pageParamDto.getParamMapObj().get(key));
                }
            }
        }
        logger.info(JSONObject.toJSONString(queryWrapper));
        queryWrapper.orderByDesc("update_time");
        Page<News> entityPage = newsService.page(new Page<>(pageParamDto.getPageIndex(), pageParamDto.getPageSize()),queryWrapper);
        return HttpResultDto.success(entityPage);
    }
    @GetMapping(value="/detail")
    public HttpResultDto<News> detail(String id){
        News news = newsService.getById(id);
        logger.info(JSONObject.toJSONString(news));

        return HttpResultDto.success(news);
    }

    /**
     * 批量导入文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload-file")
    public HttpResultDto<String> uploadFile(@RequestPart("file") MultipartFile file){
        JwtUser currentUser = AuthUtil.getCurrentJwtUser();
        if(currentUser == null){
            return HttpResultDto.failed("无权限");
        }
        //判断文件是否为空
        if(file.isEmpty()){
            return HttpResultDto.failed("文件不能为空");
        }
        try {
            // 获取文件名
            String fileName = file.getOriginalFilename();

            // 获取文件的字节
            byte[] bytes = file.getBytes();

            // 这里可以添加保存文件的代码，例如将文件保存到磁盘或数据库
            // 保存文件到本地的路径
            String savePath = "/lengyu/nginx/html/download/wow_collect/news/" + fileName;
            // 保存文件
            file.transferTo(Paths.get(savePath).toFile());
            String avatarUrl = "https://wow.shuzhipeixian.com:12812/download/wow_collect/news/"+fileName;
            return HttpResultDto.success(avatarUrl);
        } catch (Exception e) {
            return HttpResultDto.failed(e.getMessage());
        }
    }
}
